package com.jpxx.jpscp.admin.system.controller;

import com.jpxx.jpscp.admin.common.base.BaseController;
import com.jpxx.jpscp.admin.system.entity.Authority;
import com.jpxx.jpscp.admin.system.entity.Role;
import com.jpxx.jpscp.admin.system.service.AuthorityService;
import com.jpxx.jpscp.admin.system.service.RoleService;
import com.jpxx.jpscp.admin.utils.ReflectUtil;
import com.jpxx.jpscp.core.result.JsonResult;
import com.jpxx.jpscp.core.result.PageResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 权限管理
 **/
@Controller
@RequestMapping("/system/authority")
public class AuthorityController extends BaseController {
    @Autowired
    private AuthorityService authorityService;
    @Autowired
    private RoleService roleService;

    @RequiresPermissions("authority:view")
    @RequestMapping()
    public String authority(Model model) {
        List<Role> roles = roleService.list(false);
        model.addAttribute("roles", roles);
        return "system/authority.html";
    }

    @RequestMapping("editForm")
    public String editForm(Model model) {
        List<Authority> authority = authorityService.listMenu();
        model.addAttribute("authority", authority);
        return "system/authority_form.html";
    }

    /**
     * 查询所有权限
     **/
    @RequiresPermissions("authority:view")
    @ResponseBody
    @RequestMapping("/list")
    public PageResult<Map<String, Object>> list(String roleId) {
        List<Map<String, Object>> maps = new ArrayList<>();
        List<Authority> authorities = authorityService.list();
        List<Authority> roleAuths = authorityService.listByRoleId(roleId);
        for (Authority one : authorities) {
            Map<String, Object> map = ReflectUtil.objectToMap(one);
            map.put("checked", 0);
            for (Authority roleAuth : roleAuths) {
                if (one.getId().equals(roleAuth.getId())) {
                    map.put("checked", 1);
                    break;
                }
            }
            maps.add(map);
        }
        return new PageResult<>(maps);
    }
    /**
     * 添加权限
     */
    @RequiresPermissions("authority:add")
    @ResponseBody
    @RequestMapping("/add")
    public JsonResult add(Authority authority) {
        if (authorityService.add(authority)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    /**
     * 修改权限
     */
    @RequiresPermissions("authority:edit")
    @ResponseBody
    @RequestMapping("/update")
    public JsonResult update(Authority authority) {
        if (authorityService.update(authority)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    /**
     * 删除权限
     */
    @RequiresPermissions("authority:delete")
    @ResponseBody
    @RequestMapping("/delete")
    public JsonResult delete(String authorityId) {
        if (authorityService.delete(authorityId)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

}
